//
//  HDDatePickerViewController.m
//  HDVideoKitDemok
//
//  Created by LiuGaoSheng on 2021/9/2.
//  Copyright © 2021 刘高升. All rights reserved.
//

#import "HDDatePickerViewController.h"
#import "HDDatePickerView.h"
#import "Macro.h"

@interface HDDatePickerViewController ()<HDDatePickerViewDelegate>
@property (weak, nonatomic) HDDatePickerView *dateView;
@end

@implementation HDDatePickerViewController
- (instancetype)init
{
    if (self = [super init])
    {

        self.modalPresentationStyle = UIModalPresentationOverFullScreen;
        self.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initSubViews];
}

- (void)initSubViews {
    self.view.backgroundColor = RGBA(0, 0, 0, 0.5);
    HDDatePickerView *dateView = [[HDDatePickerView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, 300)];
    dateView.delegate = self;
    dateView.title = @"请选择预约时间";
    dateView.minuteInterval = 15;
    [self.view addSubview:dateView];
    self.dateView = dateView;
    [UIView animateWithDuration:0.3 animations:^{
        self.dateView.frame = CGRectMake(0, self.view.frame.size.height - 300, self.view.frame.size.width, 300);
        [self.dateView show];
    }];
}

#pragma mark - HDDatePickerViewDelegate
/**
 保存按钮代理方法

 @param timer 选择的数据
 */
- (void)datePickerViewSaveBtnClickDelegate:(NSString *)timer {
    NSLog(@"保存点击");
    NSDate *date = [self getTimeStrWithString:timer];
    NSInteger dateTime = [self getDateTimeTOMilliSeconds:date];
    if ([self compareTwoTime:dateTime time2:[self getDateTimeTOMilliSeconds:[NSDate date]]] == YES) {
        if ([self.delegate respondsToSelector:@selector(datePickerViewSaveBtnClickDelegate:)]) {
            [self.delegate datePickerViewSaveBtnClickDelegate:timer];
        }
        [self dismissViewControllerAnimated:NO completion:nil];
    }else {
        [SVProgressHUD showToast:@"请选择大于当前的时间"];
    }

}

/**
 取消按钮代理方法
 */
- (void)datePickerViewCancelBtnClickDelegate {
    NSLog(@"取消点击");
    [UIView animateWithDuration:0.3 animations:^{
        self.dateView.frame = CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, 300);
    }completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:nil];
    }];
}
#pragma mark - time 处理
//date转化时间戳
-(NSInteger)getDateTimeTOMilliSeconds:(NSDate *)datetime {

    NSTimeInterval interval = [datetime timeIntervalSince1970];
    return interval;
}


///时间戳转化为字符转0000-00-00 00:00
- (NSString *)time_timestampToString:(NSInteger)timestamp{
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timestamp];
    NSDateFormatter *dateFormat=[[NSDateFormatter alloc]init];
     [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSString* string=[dateFormat stringFromDate:confromTimesp];
    return string;
}

- (NSDate *)getTimeStrWithString:(NSString *)str {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSDate *birthdayDate = [dateFormatter dateFromString:str];
    return birthdayDate;
}

- (BOOL)compareTwoTime:(NSInteger )time1 time2:(NSInteger )time2 {

        
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
        
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:time1];
    NSString *dateString1 = [formatter stringFromDate:date];
        
    NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:time2];
    NSString *dateString2 = [formatter stringFromDate:date2];
        
    // 日历对象（方便比较两个日期之间的差距）
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSCalendarUnit unit =NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute;
    NSDateComponents *cmps = [calendar components:unit fromDate:date2 toDate:date options:0];

    NSLog(@"dateString1=%@",dateString1);
    NSLog(@"dateString2=%@",dateString2);
    NSString *str = [NSString stringWithFormat:@"差值%ld天,%ld小时%ld分%ld秒",(long)cmps.day ,(long)cmps.hour, (long)cmps.minute,(long)cmps.second];
    NSLog(@"1111=%@",str);
    if (cmps.day < 0 || cmps.hour < 0 || cmps.minute < 0) {
        return NO;
    }else {
        return YES;
    }

//    NSLog(@"%@",);

//    // 获得某个时间的年月日时分秒
//    NSLog(@"差值%ld天,%ld小时%ld分%ld秒",cmps.day ,cmps.hour, cmps.minute,cmps.second);
//    输出结果：
//    2017-06-30 10:39:32
//    2017-07-15 23:59:00
//    差值15天,13小时19分28秒
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
